# -*- coding: utf-8 -*-

# -------------------------------
# Author    : linlingwei
# Email     : 951035650@qq.com
# File Name : name_editor.py
# Date      : 2019/7/31 
# IDE       : PyCharm
# Version   : 1.0.2
# -------------------------------
# ----------- update ------------
# 1.0.2 : 添加批量改名
# -------------------------------
import maya.cmds as mc
import maya.mel as mel

import scripts.commonCmds as con
import editors.editor as editor
reload(editor)
class NameEditor(editor.Editor):
    def __init__(self):
        super(NameEditor,self).__init__()
        
    def win(self):
        
        _win = self.selfWindow()
        mc.menuBarLayout(bgc=self.rgbConvert(118, 137, 143))
        mc.menu("File",)
        mc.menuItem()
        mc.setParent("..")
        inlayout = mc.columnLayout(adj=True)
        
        self.main_layout(inlayout)
        
        # mc.text(self.text("ddddd"))
        self.copyright()
        mc.setParent("..")
        mc.showWindow(_win)
        return
    
    def main_layout(self,inlayout):
        
        layout = mc.columnLayout(adj=True, bgc=self.rgbConvert(157, 181, 193))
        # _win = self.selfWindow()
        
        fullName1 = con.lname(con.selfFuncName(),"prefix")
        mc.textFieldButtonGrp(fullName1,l=self.text("Prefix : "), cw3=[120, 100, 5, ], bl="Apply",
                              bc=lambda *args:self.btn_prefix(fullName1))
        
        fullName2 = con.lname(con.selfFuncName(),"suffix")
        mc.textFieldButtonGrp(fullName2,l=self.text("Suffix : "), cw3=[120, 100, 5, ], bl="Apply",
                              bc=lambda *args: self.btn_suffix(fullName2))
        
        mc.separator()
        sbtn = mc.textFieldGrp(l=self.text("Search for : "),cw2=[120,140])
        rbtn = mc.textFieldGrp(l=self.text("Replace With : "),cw2=[120,140])
        
        mc.rowLayout(nc=10)
        mc.text("               ")
        collection1 = mc.radioCollection()
        rb1 = mc.radioButton('hierarchy',label='hierarchy')
        rb2 = mc.radioButton('selected',label='selected')
        rb3 = mc.radioButton('all',label='all')
        mc.radioCollection(collection1,e=True,sl="hierarchy")
        mc.setParent('..')
        mc.button(l="Apply",c=lambda *args:self.btn_replace(sbtn,rbtn,collection1))

        mc.separator()
        
        fullName3 = con.lname(con.selfFuncName(), "rename")
        mc.textFieldButtonGrp(fullName3,l=self.text("Rename : "), cw3=[120, 100, 5, ], bl="Apply",
                              bc=lambda *args: self.btn_rename(fullName3))
        
        mc.separator()

        mc.setParent("..")
        
        return layout
    
    def getText(self,control):
        return mc.textFieldButtonGrp(control,q=True,tx=True)

    def btn_prefix(self,control):
        tx = self.getText(control)
        
        sel = mc.ls(sl=True)
        if sel:
            for sl in sel:
                mc.rename(sl,con.lname(tx,sl))
        
        return

    def btn_suffix(self,control):
        tx = self.getText(control)
    
        sel = mc.ls(sl=True)
        if sel:
            for sl in sel:
                mc.rename(sl, con.lname(sl, tx))
    
        return
    
    def btn_replace(self,control1,control2,control3):
        
        stx = mc.textFieldGrp(control1,q=True,tx=True)
        rtx = mc.textFieldGrp(control2,q=True,tx=True)
        choose = mc.radioCollection(control3,q=True,sl=True)
        
        mel.eval("searchReplaceNames {} {} {} ;".format(stx,rtx,choose))

        return
    
    def btn_rename(self,control):
        
        tx = self.getText(control)

        sel = mc.ls(sl=True)
        if sel:
            for i,sl in enumerate(sel):
                
                mc.rename(sl, tx.replace("@",str(i)))
        return
    
    
def showEditor():
    NameEditor().win()
    


if __name__ == '__main__':
    pass